package com.example.xiaomaotv;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.xiaomaotv.adapter.MessageAdapter;
import com.example.xiaomaotv.entity.Message;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class MessageFragment extends Fragment {
    private List<Message> messageData = new ArrayList<>();
    private ListView messageList;
    private MessageAdapter messageAdapter;
    private View messageView;
    private TextView dianzan,guanzhu,pinglun;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        messageView = inflater.inflate(R.layout.message_fragment, container, false);
        dianzan =messageView.findViewById(R.id.text_diannzan);
        guanzhu = messageView.findViewById(R.id.text_guanzhu);
        pinglun = messageView.findViewById(R.id.text_pinglun);

        // 初始化消息数据
        initMessageData();
        // 初始化ListView和适配器
        initListView();
        setupListItemClickListener();
        clickTop();
        return messageView;
    }
    public void initListView(){
        messageList = messageView.findViewById(R.id.message_list);
        messageAdapter = new MessageAdapter(getActivity(), R.layout.item_message, messageData);
        messageList.setAdapter(messageAdapter);
        setupListItemClickListener();
    }
    // 初始化消息数据
    private void initMessageData() {
        messageData.clear();
        // 添加消息数据
        messageData.add(new Message(R.drawable.message_title7, "系统消息", "小猫影视欢迎你！！！", "2025/6/18"));
        messageData.add(new Message(R.drawable.message_title8, "客服消息", "我是你的专属客服！", "2025/6/18"));
        messageData.add(new Message(R.drawable.message_title3, "沈小兔", "哈哈哈", "2025/6/18"));
        messageData.add(new Message(R.drawable.message_title2, "沈小狗", "你好！", "2025/6/17"));
        messageData.add(new Message(R.drawable.message_title5, "韩小猫", "我是韩小猫！", "2025/6/16"));
        messageData.add(new Message(R.drawable.message_title6, "沈小明", "在不在！", "2025/6/15"));
        messageData.add(new Message(R.drawable.message_title1, "韩小红", "你好！", "2025/6/15"));
    }
    // 设置列表项点击事件
    private void setupListItemClickListener() {
        messageList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Message selectedItem = messageData.get(position);
                // 创建Intent跳转到聊天界面
                Intent intent = new Intent(getActivity(), MainActivity4.class);
                intent.putExtra("Name", selectedItem.getName());
                intent.putExtra("Img", selectedItem.getImg());
                intent.putExtra("Content",selectedItem.getContent());
                startActivity(intent);
            }
        });
    }
    private void clickTop(){
        dianzan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intentTop("赞和收藏");
            }
        });
        guanzhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intentTop("新增关注");
            }
        });
        pinglun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intentTop("评论和@");
            }
        });
    }
    public void intentTop(String text){
        Intent intent = new Intent(getActivity(), MainActivity5.class);
        intent.putExtra("TEXT",text);
        startActivity(intent);
    }
}